package com.hyfrogx.modules.job.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyfrogx.modules.app.utils.FrogConstant;
import com.hyfrogx.modules.job.dao.HyAdJobDao;
import com.hyfrogx.modules.job.entity.HyAdJobEntity;
import com.hyfrogx.modules.job.service.HyAdJobService;
import com.hyfrogx.modules.job.utils.HyAdJobUtils;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0.0
 * @Author: lfh
 * @Date: 2019/10/18 14:43
 * @Company 汉咏科技
 */
@Service("hyAdJobService")
public class HyAdJobServiceImpl extends ServiceImpl<HyAdJobDao, HyAdJobEntity> implements HyAdJobService {

    @Autowired
    private Scheduler scheduler;
    /**
     * 项目启动时，初始化定时器
     */
    @PostConstruct
    public void init(){
        List<HyAdJobEntity> adJobEntityList = this.list(new QueryWrapper<HyAdJobEntity>().eq("status",FrogConstant.AdJobStatus.USING.getValue()));
        for(HyAdJobEntity entity : adJobEntityList){
            CronTrigger cronTrigger = HyAdJobUtils.getCronTrigger(scheduler, entity.getJobId());
            //如果不存在，则创建
            if(cronTrigger == null) {
                HyAdJobUtils.createScheduleJob(scheduler, entity);
            }
            else {
                HyAdJobUtils.updateScheduleJob(scheduler, entity);
            }
        }
    }

    @Override
    public void saveAdJob(HyAdJobEntity hyAdJobEntity) {
        hyAdJobEntity.setCreateTime(new Date());
        hyAdJobEntity.setStatus(FrogConstant.AdJobStatus.USING.getValue());
        this.save(hyAdJobEntity);
        //创建定时任务
        HyAdJobUtils.createScheduleJob(scheduler,hyAdJobEntity);
    }

    @Override
    public void deleteAdJob(Integer id) {
        //根据参数（params)id查询任务
        HyAdJobEntity hyAdJobEntity = this.getOne(new QueryWrapper<HyAdJobEntity>().eq("params",String.valueOf(id)).eq("status",FrogConstant.AdJobStatus.USING.getValue()));
        Assert.notNull(hyAdJobEntity,"数据库中不存在此广告任务");
        //设置状态为无用
        hyAdJobEntity.setStatus(FrogConstant.AdJobStatus.NOUSING.getValue());
        this.updateById(hyAdJobEntity);
        //删除JobId定时任务
        HyAdJobUtils.deleteScheduleJob(scheduler,hyAdJobEntity.getJobId());
    }

    @Override
    public boolean queryAdJob(Integer id) {
        HyAdJobEntity hyAdJobEntity = this.getOne(new QueryWrapper<HyAdJobEntity>().eq("params",String.valueOf(id)).eq("status",FrogConstant.AdJobStatus.USING.getValue()));
        if (ObjectUtil.isNotNull(hyAdJobEntity))
        {
            return true;
        }
        else {
            return false;
        }
    }
}
